package com.jicheng.Utils;

import com.jicheng.config.properties.ServerProperties;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;

/**
 * @ClassName: ServerUtil
 * @Description: TODO
 * @author: khz
 * @date: 2020年09月24日 16:03
 */
public class ServerUtil {
    /**
     * 获取服务部署根路径 http:// + ip + port
     *
     * @param request
     * @return
     */
//    @Autowired
//    private static ServerProperties serverProperties;
    public static String getServerIPPort(HttpServletRequest request) {
//        return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
        ServerProperties serverProperties = (ServerProperties) SpringUtils.getBean("serverProperties");
        String port = serverProperties.getPort();
        InetAddress ia = null;
        try {
            ia = ia.getLocalHost();
            String localname = ia.getHostName();
            String localip = ia.getHostAddress();
            System.out.println("本机名称是：" + localname);
            System.out.println("本机的ip是 ：" + localip);
            return request.getScheme() + "://" + localip + ":" + port;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取请求ip
     * @param request
     * @return
     */
    public static String getRemortIP(HttpServletRequest request) {
        if (request.getHeader("x-forwarded-for") == null) {
            return request.getRemoteAddr();
        }
        return request.getHeader("x-forwarded-for");
    }

}
